package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.VoyageForm;
import com.vcaresolution.logistic.pojo.Port;
import com.vcaresolution.logistic.pojo.Sector;
import com.vcaresolution.logistic.pojo.Vessel;
import com.vcaresolution.logistic.pojo.Voyage;
import com.vcaresolution.logistic.to.VoyageTO;
import com.vcaresolution.util.Utils;

@Service
public class VoyageServiceImpl extends BaseService implements VoyageService{

	private Voyage populateVoyagePojo(VoyageForm voyageForm){
		Voyage voyage = new Voyage();
		BeanUtils.copyProperties(voyageForm, voyage);
		
		return voyage;
	}
	
	@Override
	@Transactional
	public void add(VoyageForm voyageForm) {
		Voyage voyage = populateVoyagePojo(voyageForm);

		voyage.setPort(getDaoFactory().getUtilityDAO().findById(Port.class, voyageForm.getPortid()));
		voyage.setVessel(getDaoFactory().getUtilityDAO().findById(Vessel.class, voyageForm.getVesselid()));
		voyage.setSector(getDaoFactory().getUtilityDAO().findById(Sector.class, voyageForm.getSectorid()));
		
		voyage.setAuditdate(Utils.getCurrentDate());
		
		getDaoFactory().getUtilityDAO().add(voyage);
	}

	@Override
	@Transactional
	public void update(VoyageForm voyageForm) {
		Voyage voyage = populateVoyagePojo(voyageForm);
		
		voyage.setPort(getDaoFactory().getUtilityDAO().findById(Port.class, voyageForm.getPortid()));
		voyage.setVessel(getDaoFactory().getUtilityDAO().findById(Vessel.class, voyageForm.getVesselid()));
		voyage.setSector(getDaoFactory().getUtilityDAO().findById(Sector.class, voyageForm.getSectorid()));
		
		voyage.setAuditdate(Utils.getCurrentDate());
		
		getDaoFactory().getUtilityDAO().update(voyage);
	}

	@Override
	@Transactional
	public void delete(VoyageForm voyageForm) {
		getDaoFactory().getUtilityDAO().delete(populateVoyagePojo(voyageForm));
	}

	@Override
	@Transactional
	public VoyageTO findById(Serializable voyageID) {
		Voyage voyage = (Voyage) getDaoFactory().getUtilityDAO().findById(Voyage.class,voyageID);
		
		VoyageTO voyageTO = new VoyageTO();

		BeanUtils.copyProperties(voyage, voyageTO);
		
		voyageTO.setVesselid(voyage.getVessel().getVesselid());
		voyageTO.setVesselname(voyage.getVessel().getVesselname());
		
		voyageTO.setPortid(voyage.getPort().getPortid());
		voyageTO.setPortname(voyage.getPort().getPortname());

		voyageTO.setSectorid(voyage.getSector().getSectorid());
		voyageTO.setSectorname(voyage.getSector().getSectorname());
		return voyageTO; 
	}

	@Override
	@Transactional
	public List<VoyageTO> findByAll() {
		List<Voyage> voyageList = getDaoFactory().getUtilityDAO().findByAll(Voyage.class);
		
		List<VoyageTO> voyageFormList = new ArrayList<VoyageTO>();
		
		if( voyageList != null && !voyageList.isEmpty() ){
			VoyageTO voyageForm = null;
			for(Voyage voyage: voyageList){
				voyageForm = new VoyageTO();
				
				BeanUtils.copyProperties(voyage, voyageForm);
				
				// Show Vesssel Name in List
				voyageForm.setVesselname(voyage.getVessel().getVesselname());
				// Show Port of Loading in List
				voyageForm.setPortname(voyage.getPort().getPortname());
				// Show Sector in List
				voyageForm.setSectorname(voyage.getSector().getSectorname());
				
				voyageFormList.add(voyageForm);
			}
		}	
		return voyageFormList;
	}

	@Override
	@Transactional
	public List<VoyageTO> findVoyageWithVessel() {
		List<Voyage> voyageList = getDaoFactory().getUtilityDAO().findByAll(Voyage.class);
		
		List<VoyageTO> voyageFormList = new ArrayList<VoyageTO>();
		
		if( voyageList != null && !voyageList.isEmpty() ){
			VoyageTO voyageTO = null;
			for(Voyage voyage: voyageList){
				voyageTO = new VoyageTO();
				
				BeanUtils.copyProperties(voyage, voyageTO);
				
				// Show Vesssel Name in List
				voyageTO.setVesselVoyagePOL(voyage.getVessel().getVesselname()+"-"+voyage.getVoyagename()+" ["+voyage.getPort().getPortname()+"]");
				
				voyageFormList.add(voyageTO);
			}
		}	
		return voyageFormList;
	}
}